(function(win, factory) {
	if ('function' === typeof define && define.amd) {
		define(function() {
			return factory();
		});
	} else {
		factory(win);
	}
}(window, function(win) {

	/* 
	 * author:gtShen
	 * QQ:903050269
	 * email:sgt_ah@163.com
	 * version:0.5
	 * license:MIT
	 */

	var Method = {

		getClass: function(className) {

			var elements = [];
			var allElement;
			var pattern = new RegExp('\\s*' + className + '\\s*');

			if (document.getElementsByClassName) {
				return document.getElementsByClassName(className);
			}

			if (document.querySelector) {
				return document.querySelector(className);
			} else {

				allElement = document.getElementsByTagName('*');

				for (var i in allElement) {
					if (pattern.test(allElement[i].className)) {
						elements.push(allElement[i]);
					}
				}

				return elements;
			}

		},
		getPos: function(obj) {

			var offsetLeft = obj.offsetLeft;
			var offsetTop = obj.offsetTop;
			var offsetWidth = obj.offsetWidth;
			var offsetHeight = obj.offsetHeight;
			var parentElement = obj.offsetParent;

			while (offsetLeft += parentElement.offsetLeft, offsetTop += parentElement.offsetTop, parentElement = parentElement.offsetParent) {;
			}
			return {
				'left': offsetLeft,
				'top': offsetTop,
				'width': offsetWidth,
				'height': offsetHeight
			};
		},
		removeTips: function(className) {
			var element = document.getElementById(className);
			if (element && element.parentNode && element.tagName != 'BODY') {
				element.parentNode.removeChild(element);
			}
		},

		core: function(img, box, pos, params) {

			var width = img.width || img.offsetWidth;
			var height = img.height || img.offsetHeight;
			var left = pos.left;
			var top = pos.top;
			var tw = pos.width;
			var th = pos.height;
			var ch = document.documentElement.clientHeight || document.body.clientHeight;
			var cw = document.documentElement.clientWidth || document.body.clientWidth;

			if (width + left > cw) {
				box.style.left = left - width + tw + 'px';
			}


			if(ch - (th+top) <  height){
				box.style.top =  top - height + 'px';
			}

			box.innerHTML = '';
			box.appendChild(img);

			if (!params.loading) {
				document.body.appendChild(box);
			}
		}

	};

	function tooltip(params) {



		var params = params || {};
		var rel = params.rel || 'rel';
		var tooltipContentBox = params.tooltipBox || 'tooltip-content-box';
		var elements = Method.getClass(params.className || 'tooltip');
		

		for (var i = 0; i < elements.length; i++) {

			elements[i].onmouseover = function(e) {

				var pos = Method.getPos(this);
				var img = new Image();
				var source = this.getAttribute(rel);
				var oDiv = document.createElement('DIV');

				oDiv.id = tooltipContentBox;
				oDiv.style.cssText = 'position:absolute;left:' + pos.left + 'px;top:' + (pos.top + pos.height) + 'px;z-index:999999';
				
				if (params && params.loading) {
					
					oDiv.innerHTML = '<img src="' + params.loading + '" />';
					document.body.appendChild(oDiv);
				}

				if (source) {

					if (window.attachEvent) {
						img.attachEvent('onload', function() {
							Method.core(img, oDiv, pos, params);
						});
					} else {
						img.onload = function() {
							Method.core(img, oDiv, pos, params);
						}
					}

					img.onerror = img.onabort = function() {
						Method.core(img, oDiv, pos, params);
					};

					img.src = source;

				}

			};

			elements[i].onmouseout = function() {
				Method.removeTips(tooltipContentBox);
			}

		}
	}


	if ('function' === typeof define && define.amd) {
		return tooltip;
	} else if ('function' === typeof $ && $) {
		$.tooltip = function(params) {
			return tooltip(params);
		}
	} else {
		win.tooltip = function(params) {
			return tooltip(params);
		}
	}

}));